//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CapletVarianceCurve.h"
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletVolatilityStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures::Volatility;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::CCapletVarianceCurve (DateTime referenceDate, Cephei::IVector<DateTime>^ dates, Cephei::IVector<Double>^ capletVolCurve, Cephei::QL::Times::IDayCounter^ dayCounter) : COptionletVolatilityStructure(CCapletVarianceCurve::typeid)
{
    CoVector<DateTime>^ _Cdates;
    CoVector<Double>^ _CcapletVolCurve;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phCapletVarianceCurve = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate);
        CoVector<DateTime>^ _Cdates = safe_cast<CoVector<DateTime>^> (dates);
        _Cdates->Lock();
        INativeVector<DateTime>^ _NCIdates = _Cdates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCdates = safe_cast<CDateTimeVector^>(_NCIdates);
        std::vector<QuantLib::Date>& _dates = static_cast<std::vector<QuantLib::Date>&> (_NCdates->GetReference ());
        CoVector<Double>^ _CcapletVolCurve = safe_cast<CoVector<Double>^> (capletVolCurve);
        _CcapletVolCurve->Lock();
        INativeVector<Double>^ _NCIcapletVolCurve = _CcapletVolCurve->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcapletVolCurve = safe_cast<CDoubleVector^>(_NCIcapletVolCurve);
        std::vector<QuantLib::Volatility>& _capletVolCurve = static_cast<std::vector<QuantLib::Volatility>&> (_NCcapletVolCurve->GetReference ());
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppCapletVarianceCurve = new boost::shared_ptr<QuantLib::CapletVarianceCurve> (new QuantLib::CapletVarianceCurve ( _referenceDate,  _dates,  _capletVolCurve,  _dayCounter ));
        SetOptionletVolatilityStructure (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppCapletVarianceCurve));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdates != nullptr) _Cdates->Unlock();
        if (_CcapletVolCurve != nullptr) _CcapletVolCurve->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::CCapletVarianceCurve (boost::shared_ptr<QuantLib::CapletVarianceCurve>& childNative, Object^ owner) : COptionletVolatilityStructure(CCapletVarianceCurve::typeid)
{
#ifdef HANDLE
	_phCapletVarianceCurve = NULL;
#endif
	_ppCapletVarianceCurve = &childNative;
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppCapletVarianceCurve));
}
Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::CCapletVarianceCurve (QuantLib::CapletVarianceCurve& childNative, Object^ owner) : COptionletVolatilityStructure(CCapletVarianceCurve::typeid)
{
#ifdef HANDLE
	_phCapletVarianceCurve = NULL;
#endif
	_ppCapletVarianceCurve = new boost::shared_ptr<QuantLib::CapletVarianceCurve> (&childNative);
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppCapletVarianceCurve));
    _CapletVarianceCurveOwner = owner;
    _OptionletVolatilityStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::CCapletVarianceCurve (CCapletVarianceCurve^ copy) : COptionletVolatilityStructure(CCapletVarianceCurve::typeid)
{
#ifdef HANDLE
	_phCapletVarianceCurve = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCapletVarianceCurve = new boost::shared_ptr<QuantLib::CapletVarianceCurve> (copy->GetShared());
        _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppCapletVarianceCurve));
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::CCapletVarianceCurve (System::Type^ t) : COptionletVolatilityStructure(CCapletVarianceCurve::typeid)
{
#ifdef HANDLE
	_phCapletVarianceCurve = NULL;
#endif
	if (!t->IsSubclassOf(CCapletVarianceCurve::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::CCapletVarianceCurve (QuantLib::Handle<QuantLib::CapletVarianceCurve>& childNative, Object^ owner)  : COptionletVolatilityStructure(CCapletVarianceCurve::typeid)
{
	_phCapletVarianceCurve = &childNative;
	_ppCapletVarianceCurve = &static_cast<boost::shared_ptr<QuantLib::CapletVarianceCurve>>(childNative.currentLink());
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppCapletVarianceCurve));
    _CapletVarianceCurveOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::CCapletVarianceCurve (QuantLib::Handle<QuantLib::CapletVarianceCurve> childNative)  : COptionletVolatilityStructure(CCapletVarianceCurve::typeid)
{
	_phCapletVarianceCurve = &childNative;
	_ppCapletVarianceCurve = &static_cast<boost::shared_ptr<QuantLib::CapletVarianceCurve>>(childNative.currentLink());
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppCapletVarianceCurve));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::CCapletVarianceCurve (QuantLib::CapletVarianceCurve childNative)  : COptionletVolatilityStructure(CCapletVarianceCurve::typeid)
{
#ifdef HANDLE
	_phCapletVarianceCurve = NULL;
#endif
	_ppCapletVarianceCurve = new boost::shared_ptr<QuantLib::CapletVarianceCurve> (new QuantLib::CapletVarianceCurve (childNative));
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppCapletVarianceCurve));
}
#endif

Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::~CCapletVarianceCurve ()
{
    if (_ppCapletVarianceCurve != NULL)
    {
	    delete _ppCapletVarianceCurve;
        _ppCapletVarianceCurve = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::!CCapletVarianceCurve ()
{
    if (_ppCapletVarianceCurve != NULL)
    {
	    delete _ppCapletVarianceCurve;
    }
}
QuantLib::CapletVarianceCurve& Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::GetReference ()
{
    if (_ppCapletVarianceCurve == NULL) throw gcnew NativeNullException ();
	return **_ppCapletVarianceCurve;
}
boost::shared_ptr<QuantLib::CapletVarianceCurve>& Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::GetShared ()
{
    if (_ppCapletVarianceCurve == NULL) throw gcnew NativeNullException ();
	return *_ppCapletVarianceCurve;
}
QuantLib::CapletVarianceCurve* Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::GetPointer ()
{
    if (_ppCapletVarianceCurve == NULL) throw gcnew NativeNullException ();
	return &**_ppCapletVarianceCurve;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CapletVarianceCurve>& Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::GetHandle ()
{
	if (_phCapletVarianceCurve == NULL)
	{
		_phCapletVarianceCurve = new Handle<QuantLib::CapletVarianceCurve> (*_ppCapletVarianceCurve);
	}
	return *_phCapletVarianceCurve;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::HasNative () 
{
	return (_ppCapletVarianceCurve != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppCapletVarianceCurve)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCapletVarianceCurve)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCapletVarianceCurve)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCapletVarianceCurve)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Optionlet::ICapletVarianceCurve^ Cephei::QL::Termstructures::Volatility::Optionlet::CCapletVarianceCurve_Factory::Create (DateTime referenceDate, Cephei::IVector<DateTime>^ dates, Cephei::IVector<Double>^ capletVolCurve, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return gcnew CCapletVarianceCurve ( referenceDate,  dates,  capletVolCurve,  dayCounter);
}
